package com.yserp.project.erp.store.stock.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.store.stock.domain.Stock;
import com.yserp.project.erp.store.stock.service.IStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 商品库存信息Controller
 *
 * @author yserp
 * @date 2022-07-01
 */
@Controller
@RequestMapping("/store/stock")
public class StockController extends BaseController {
    private String prefix = "erp/store/stock";

    @Autowired
    private IStockService stockService;

    @RequiresPermissions("store:stock:view")
    @GetMapping()
    public String stock() {
        return prefix + "/stock";
    }

    /**
     * 查询商品库存信息列表
     */
    @RequiresPermissions("store:stock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Stock stock) {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 导出商品库存信息列表
     */
    @RequiresPermissions("store:stock:export")
    @Log(title = "商品库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Stock stock) {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        return util.exportExcel(list, "商品库存信息数据");
    }

    /**
     * 新增商品库存信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品库存信息
     */
    @RequiresPermissions("store:stock:add")
    @Log(title = "商品库存信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Stock stock) {
        return toAjax(stockService.save(stock));
    }

    /**
     * 修改商品库存信息
     */
    @RequiresPermissions("store:stock:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Stock stock = stockService.getById(id);
        mmap.put("stock", stock);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品库存信息
     */
    @RequiresPermissions("store:stock:edit")
    @Log(title = "商品库存信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Stock stock) {

        return toAjax(stockService.updateById(stock));
    }

    /**
     * 删除商品库存信息
     */
    @RequiresPermissions("store:stock:remove")
    @Log(title = "商品库存信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stockService.removeByIds(Arrays.asList(Convert.toLongArray(ids))));
    }


    /**
     * 选择库存
     */
    @GetMapping("/selectStock")
    public String selectStock(@RequestParam("warehouseId") Long warehouseId, ModelMap mmap) {
        mmap.put("warehouseId", warehouseId);
        return prefix + "/selectStock";
    }

    /**
     * 查询库存列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(Stock stock) {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }
}
